package com.traffic.controller.trafficcar;

import com.ainsurtech.proxy.Response;
import com.ainsurtech.proxy.demo.TrafficCarPage;
import com.traffic.entity.TrafficCar;
import com.traffic.trafficCar.TrafficCarService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

/**
 * Created by Administrator on 2018/4/6.
 */
@RestController
@RequestMapping("/trafficcar/")
public class TrafficCarController {

    @Autowired
    private TrafficCarService service;




    /**
     * 添加对象
     * @param dto 对象
     * @return
     * @ApiOperation为添加的API相关注解
     *
     */
    @ApiOperation(value = "样例添加",httpMethod = "POST", notes = "暂无")
    @PostMapping("add")
    public Response<Void> add(@ApiParam(required = true, name = "dto", value = "添加对象信息") @RequestBody TrafficCarDTO dto) {
        TrafficCar entity = new TrafficCar();
        BeanUtils.copyProperties(dto,entity);
        this.service.addTraffic(entity);
        return new Response<>();
    }

    /**
     * 更新对象
     *
     * @param dto 更新对象
     * @return
     */
    @ApiOperation(value = "样例更新",httpMethod = "PUT", notes = "暂无")
   // @RequestMapping(value = "{id}",method = RequestMethod.PUT)
    @PostMapping("update")
    public Response<Void> update(@RequestBody TrafficCarDTO dto) {
       TrafficCar entity = service.get(dto.getId());

       BeanUtils.copyProperties(dto,entity);
        this.service.updateTraffic(entity);
        return new Response<>();
    }

    /**
     * 删除对象
     *
     * @return
     */
    @ApiOperation(value = "样例删除",httpMethod = "DELETE", notes = "暂无")
    @PostMapping("del")
    public Response<Void> delete(@RequestBody TrafficCarDTO dto) {
        this.service.delTraffic(dto.getId());
        return new Response<>();
    }



    /**
     * 查询列表(分页)
     * @param page 分页对象
     * @param line 线路
     * @return
     */
    @ApiOperation(value = "样例列表",httpMethod = "GET", notes = "暂无")
    @GetMapping("findAll")
    public Response<Page<TrafficCarPage>> list(@ApiParam(required = true, name = "page", value = "分页对象") @PageableDefault Pageable page,
                                               @ApiParam(required = false, name = "name", value = "姓名") @RequestParam(required = false) String line) {

        Page<TrafficCar> pages = service.findTrafficCar(page);
        Page<TrafficCarPage> respS = pages.map(new Converter<TrafficCar, TrafficCarPage>() {
            public TrafficCarPage convert(TrafficCar source) {
                TrafficCarPage resp = new TrafficCarPage();
                if (source != null) {
                    BeanUtils.copyProperties(source, resp);
                }
                return resp;
            }
        });
        return new Response<>(respS);
    }



    /**
     * 查询列表(分页)
     * @param page 分页对象
     * @param line 线路
     * @return
     */
    @ApiOperation(value = "样例列表",httpMethod = "GET", notes = "暂无")
    @GetMapping("findAllByLineOrCar")
    public Response<Page<TrafficCarPage>> list2(@ApiParam(required = true, name = "page", value = "分页对象") @PageableDefault Pageable page,
                                               @ApiParam(required = false, name = "name", value = "姓名") @RequestParam(required = false) String line,@RequestParam(required = false) String car) {

        Page<TrafficCar> pages = service.findAllByLineOrCar(line,car,page);
        Page<TrafficCarPage> respS = pages.map(new Converter<TrafficCar, TrafficCarPage>() {
            public TrafficCarPage convert(TrafficCar source) {
                TrafficCarPage resp = new TrafficCarPage();
                if (source != null) {
                    BeanUtils.copyProperties(source, resp);
                }
                return resp;
            }
        });
        return new Response<>(respS);
    }

}
